package soccerscope.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.*;

import soccerscope.db.GameItem;
import soccerscope.db.DatabaseReader;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import soccerscope.util.analyze.DBAnalyzer;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class StatisticFrame
    extends JFrame {

  /**
   * 
   */
  private static final long serialVersionUID = -2833641598751187249L;
  BorderLayout              borderLayout1    = new BorderLayout();
  JSplitPane                jSplitPane1      = new JSplitPane();
  JScrollPane               jScrollPane1     = new JScrollPane();
  JScrollPane               jScrollPane2     = new JScrollPane();
  JList                     jGameList        = new JList();
  JTable                    jStatisticTable  = new JTable();

  public StatisticFrame() {

    try {
      this.jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
    this.loadGameName();
    this.jGameList.setModel(this.gameItems);
    TableModel dataModel = new AbstractTableModel() {

      /**
       * 
       */
      private static final long serialVersionUID = 505900208421353467L;

      public int getColumnCount() {

        return 10;
      }

      public int getRowCount() {

        return 10;
      }

      public Object getValueAt(int row,
          int col) {

        return new Integer(row * col);
      }
    };
    /*
     * String[] columnNames = {"", "Left team", "Right Team"}; DBAnalyzer
     * dbanalyzer = new DBAnalyzer();
     * 
     * Object[][] data = { {"Goal", dbanalyzer.goalAnalyze(17),
     * dbanalyzer.goalAnalyze(17)}, {"Goal Kick", dbanalyzer.goalAnalyze(17),
     * dbanalyzer.goalAnalyze(17)} };
     * 
     * jStatisticTable.setModel(new DefaultTableModel(dbanalyzer.analyze(17),
     * columnNames));
     */
  }

  private void loadGameName() {

    this.gameItems = new DefaultListModel();
    DatabaseReader dbReader = new DatabaseReader(null);
    this.gis = dbReader.getGameInfos();
    for (GameItem i : this.gis) {
      this.gameItems.addElement(i.getDescription());
    }
  }

  private void jbInit() throws Exception {

    this.getContentPane().setLayout(this.borderLayout1);
    this.setTitle("Statistic");
    this.jSplitPane1.setPreferredSize(new Dimension(450, 550));
    this.jSplitPane1.setDividerSize(2);
    this.jSplitPane1.setLastDividerLocation(200);
    this.jGameList.addMouseListener(new StatisticFrame_jGameList_mouseAdapter(
        this));
    this.jSplitPane1.add(this.jScrollPane1, JSplitPane.TOP);
    this.jSplitPane1.add(this.jScrollPane2, JSplitPane.BOTTOM);
    this.getContentPane().add(this.jSplitPane1, java.awt.BorderLayout.CENTER);

    this.jScrollPane2.getViewport().add(this.jStatisticTable);
    this.jScrollPane1.getViewport().add(this.jGameList);

    this.jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
    this.pack();
    this.jSplitPane1.setDividerLocation(200);
  }

  private ArrayList<GameItem> gis;
  private DefaultListModel    gameItems;

  public void jGameList_mouseClicked(MouseEvent e) {

    int index = this.jGameList.locationToIndex(e.getPoint());
    this.jGameList.ensureIndexIsVisible(index);
    GameItem selectedGame = (GameItem) this.gis.get(index);
    String[] columnNames = { "", "Left team", "Right Team" };
    DBAnalyzer dbanalyzer = new DBAnalyzer();
    this.jStatisticTable.setModel(new DefaultTableModel(dbanalyzer
        .analyze(selectedGame.getId()), columnNames));

    // JOptionPane.showMessageDialog(this, doc.getDescription(), "Document
    // description", JOptionPane.INFORMATION_MESSAGE);

  }
}

class StatisticFrame_jGameList_mouseAdapter
    extends MouseAdapter {

  private StatisticFrame adaptee;

  StatisticFrame_jGameList_mouseAdapter(
      StatisticFrame adaptee) {

    this.adaptee = adaptee;
  }

  @Override
  public void mouseClicked(MouseEvent e) {

    this.adaptee.jGameList_mouseClicked(e);
  }
}
